package kyssion.leetcode.num101_150;

import kyssion.leetcode.util.TreeNode;

public class code129_求根到叶子节点数字之和 {
    public static void main(String[] args) {
        TreeNode treeNode = TreeNode.create(
                new int[]{
                        4, 9, 0, 5, 1
                }
        );
        System.out.println(new code129_求根到叶子节点数字之和().sumNumbers(treeNode));
    }

    public int sumNumbers(TreeNode root) {
        all = 0;
        ans(root, 0);
        return (int) all;
    }

    long all = 0;

    private void ans(TreeNode root, long i) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            all = i + all + root.val;
        }
        ans(root.left, (i + root.val) * 10);
        ans(root.right, (i + root.val) * 10);
    }
}
